package com.sgb.kjwl.viewmodel.pick;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.pick.UserInquiryForTender;
import com.sgb.kjwl.repository.PickProjectRepertory;
import com.sgb.kjwl.view.ui.activity.pick.PickInquireByLetterActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickInquireByLetterModel extends BaseViewModel {

    private final PickInquireByLetterActivity activity;
    private final PickProjectRepertory repertory;

    public ObservableField<UserInquiryForTender> userInquiryForTender = new ObservableField<UserInquiryForTender>(new UserInquiryForTender());

    public PickInquireByLetterModel(PickInquireByLetterActivity activity, PickProjectRepertory pickProjectRepertory) {
        this.activity = activity;
        repertory = pickProjectRepertory;
    }


    //上传多图片
    public void uploadMutiFileModel(File file, int type, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "图片上传中" + completeNum + "/" + activity.imgTotal + "...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.uploadRefresh(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void saveUserInquiryForJc(UserInquiryForTender userInquiryForTender) {
        userInquiryForTender.setGroupBuyNo(userInquiryForTender.getMarkId());
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().saveUserInquiryForJc(userInquiryForTender).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, "操作成功");
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 供方发起提交函询
     *
     * @param userInquiryForTender
     */
    public void saveUserInquiryForFactoryJc(UserInquiryForTender userInquiryForTender) {
        userInquiryForTender.setGroupBuyNo(userInquiryForTender.getMarkId());
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().saveUserInquiryForFactoryJc(userInquiryForTender).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, "操作成功");
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void saveUserInquiryForTender(UserInquiryForTender userInquiryForTender) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().saveUserInquiryForTender(userInquiryForTender).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void saveUserInquiryForBid(UserInquiryForTender userInquiryForTender) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().saveUserInquiryForBid(userInquiryForTender).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }


    /**
     * 新增函询-租赁
     *
     * @param userInquiryForTender
     */
    public void addInquery(UserInquiryForTender userInquiryForTender) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().addInquery(userInquiryForTender).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    /**
     * 新增函询-工队
     *
     * @param userInquiryForTender
     * @param teamName
     */
    public void addTeamInquiry(UserInquiryForTender userInquiryForTender, String teamName) {
        // 首选企业编号，如果企业编号为空，则传用户编号，两者不能同时为空，不能同时都有值
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if (companyNo.equals("") || companyNo.equals("0")) {
            userInquiryForTender.setTeamCompamyNo("");
            userInquiryForTender.setTeamUserNo(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            userInquiryForTender.setTeamCompamyNo(companyNo);
            userInquiryForTender.setTeamUserNo("");
        }
        if (!TextUtils.isEmpty(teamName)){
            userInquiryForTender.setMarkName(teamName);
        }
        userInquiryForTender.setAreacode(repertory.getAreaCode());
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().addTeamInquiry(userInquiryForTender).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(activity, response.body().getMsg());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void tenderInquiryType(int tenderingType) {
        HashMap map = new HashMap();
        map.put("tenderingType", tenderingType);
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().tenderInquiryTypeByTenderType(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {

            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.setInquiryType(data.getData());
                } else {
                    if (!TextUtils.isEmpty(data.getMsg())) {
                        MToast.showToast(activity, data.getMsg());
                    }
                }
            }
        });

    }

}
